<template>
  <div class="logform">
    <div class="form">
      <h1>欢迎登录</h1>
      <ul>
        <li>
          <label for="">学号/工号：</label
          ><input type="text" placeholder="请输入学号/工号" v-model="id" />
        </li>
        <li>
          <label for="">密码：</label
          ><input
            type="password"
            placeholder="请输入密码"
            v-model="password"
            @keyup.enter="toLogin"
          />
        </li>
      </ul>
      <div class="button">
        <button class="log" @click="login">登录</button>
        <button class="reset" @click="reset">重置</button>
      </div>
      <br />
      <!-- <div class="reg">
        <p>没有账号？<a @click="jump">点我注册</a></p>
      </div> -->
    </div>
  </div>
</template>

<script>
import { request } from "../api/index";
import store from "../store/index";

export default {
  name: "Start",
  data() {
    return {
      id: "",
      password: "",
      tip: "",
    };
  },
  methods: {
    reset() {
      this.id = "";
      this.password = "";
    },
    login() {
      if (this.id == "" || this.password == "") {
        alert("用户名或密码不能为空！");
        return;
      }
      request({
        url: "/user/login",
        method: "post",
        data: {
          id: this.id,
          password: this.password,
        },
      })
        .then((res) => {
          // console.log(res);
          if (this.id == "" || this.password == "") {
            alert("用户名或密码不能为空！");
          } else if (res.data.message == "success") {
            this.$store.commit("store_login_message", [this.id, this.password]);
            sessionStorage.setItem("id", this.id);
            if (res.data.identity == "1") {
              window.sessionStorage.setItem("teaToken", this.id);
              this.$router.replace("/teaHome");
            } else if (res.data.identity == "0") {
              window.sessionStorage.setItem("stuToken", this.id);
              this.$router.replace("/stuhome");
            } else if (res.data.identity == "2") {
              window.sessionStorage.setItem("managerToken", this.id);
              this.$router.replace("/managerHome");
            }
          } else {
            this.tip = res.data.message;
          }
        })
        .catch((err) => {
          // console.log("异常是：" + err);
          alert("用户名或密码错误！");
          return;
        });
    },
    toLogin() {
      if (this.id != "" && this.password != "") this.login();
      else alert("请输入完整的信息！");
    },
    jump() {
      this.$router.push("/start/register");
    },
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
}
.container .logform {
  margin: 0 auto;
  width: 679px;
  height: 381px;
  background-color: rgba(252, 255, 252, 0.5);
  box-shadow: 7px 7px 17px rgba(52, 56, 66, 0.5);
  border-radius: 30px;
}
.container .logform .form {
  position: relative;
  width: 470px;
  height: 390px;
  margin: 150px auto;
}
.container .logform .form h1 {
  text-align: center;
  color: #989898;
}
.container .logform .form ul {
  width: 370px;
  height: 100px;
  margin: 30px auto;
}
.container .logform .form ul li {
  margin-bottom: 5px;
}
.container .logform .form label {
  display: inline-block;
  width: 102px;
  height: 52px;
  font-size: larger;
  font-weight: 600;
  text-align: right;
  color: #282828;
}
.container .logform .form ul input {
  margin-left: 15px;
  border-radius: 5px;
  border-style: hidden;
  height: 30px;
  width: 140px;
  background-color: rgba(216, 191, 216, 0.5);
  outline: none;
  color: #282828;
  padding-left: 10px;
}
.container .logform .form h5 {
  text-align: center;
  padding-bottom: 20px;
}
.container .logform .form .button {
  position: relative;
  background-color: inherit;
}
.container .logform .form p {
  position: absolute;
  right: -40px;
  bottom: 40px;
  font-size: 17px;
  font-weight: 700;
  color: black;
}
.container .logform .form .button .log {
  position: absolute;
  left: 68px;
  width: 100px;
  height: 31px;
  margin-top: 9px;
  background-color: #6593e3;
  border-style: hidden;
  border-radius: 5px;
  border-color: cornsilk;
  background-color: rgba(100, 149, 237, 0.7);
  color: aliceblue;
  font-size: 16px;
}
.container .logform .form .button .reset {
  position: absolute;
  right: 75px;
  width: 100px;
  height: 31px;
  margin-top: 9px;
  background-color: #6593e3;
  border-style: hidden;
  border-radius: 5px;
  border-color: cornsilk;
  background-color: rgba(100, 149, 237, 0.7);
  color: aliceblue;
  font-size: 16px;
}
.reg {
  margin-top: 25px !important;
  text-align: center;
  color: rgb(60, 126, 248);
  text-decoration: underline;
  font-size: 25px;
  font-style: initial;
  strong:hover {
    color: blue;
    cursor: pointer;
  }
}
</style>
